<?php

namespace app\admin\controller\printmanager;

use app\common\controller\Backend;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{
    
    /**
     * Order模型对象
     * @var \app\common\model\printmanager\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\printmanager\Order;
        $this->view->assign("shippingTypeList", $this->model->getShippingTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                // $row->visible(['id','user_id','amount','gold_num','gold_amount','shipping_type','shipping_price','remark','status','createtime']);

                $row->user_info = $row->user_info;
                $row->address = $row->address;
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加 
     */ 
    public function add(){
        $this->error("Error");
    }

    /**
     * 发货 
     */ 
    public function send($ids = 0){
        $item = $this->model->get($ids);
        if(!$item){
            $this->error("订单不存在");
        }



        if($item->shipping_type != 0){
            $this->error("非物流订单");
        }

        if($item->status != 1){
            $this->error("订单状态错误");
        }

        $item->status = 2;
        if($item->save()){

            $page = "/pages/ucenter/order-list/index";
            $msg = "订单号为：{$item->order_no}的订单已发货。";
            \app\common\model\SelfMessage::sendSystemMessage( $item->user_id, "订单发货", $msg, $page , $this->auth->id);

            $this->success("操作成功");
        }
        $this->error("操作失败");
    }


    /**
     * 订单关闭
     */ 
    public function close($ids = 0){
        $item = $this->model->get($ids);
        if(!$item){
            $this->error("订单不存在");
        }

        if($item->status != 0){
            $this->error("订单状态错误");
        }
        
        try {
            $res = \app\common\service\Payment::printOrderClose($item);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        
        if($res){
            $this->success("操作成功");
        }

        $this->error("操作失败");
    }

    /**
     * 订单完成 
     */ 
    public function done($ids = 0){
        $item = $this->model->get($ids);
        if(!$item){
            $this->error("订单不存在");
        }

        if($item->shipping_type == 0){
            if($item->status != 3){
                $this->error("订单状态错误");
            }
        }else{
            if($item->status != 1){
                $this->error("订单状态错误");
            }
        }

        $item->status = 5;
        if($item->save()){

            $page = "/pages/ucenter/order-list/index";
            $msg = "订单号为：{$item->order_no}的订单已完成。";
            \app\common\model\SelfMessage::sendSystemMessage( $item->user_id, "订单完成", $msg, $page , $this->auth->id);

            $this->success("操作成功");
        }
        $this->error("操作失败");
    }
}
